package com.ant.business.ocr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.SneakyThrows;
import com.ant.common.utils.FastMap;
import com.ant.common.utils.LocalBeanUtil;
import com.ant.common.utils.UserThreadLocal;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ant.business.ocr.entity.Explorer;
import com.ant.business.ocr.mapper.ExplorerMapper;
import com.ant.common.exceptions.BusinessException;
import com.ant.business.ocr.service.ExplorerService;

import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
 * Service实现类
 *
 * @description
 * @create 2023-08-16 11:20:20
 */
@Service("explorerService")
@Transactional(rollbackFor = Throwable.class)
public class ExplorerServiceImpl extends ServiceImpl<ExplorerMapper, Explorer> implements ExplorerService {

    private final ExplorerMapper explorerMapper;

    public ExplorerServiceImpl(ExplorerMapper explorerMapper) {
        this.explorerMapper = explorerMapper;
    }

    /**
     * 分页查询列表
     *
     * @param page 页数
     * @param limit 条数
     * @param param 参数
     * @return PageInfo
     */
    @Override
    public PageInfo<Explorer> getList(int page, int limit, FastMap param) {
        QueryWrapper<Explorer> wrapper = new QueryWrapper<>();
        PageHelper.startPage(page, limit);
        List<Explorer> list = explorerMapper.selectList(wrapper);
        return new PageInfo<Explorer>(list);
    }

    /**
     * 分页查询列表
     *
     * @param param 参数
     * @return PageInfo
     */
    @Override
    public List<Explorer> getAllList(FastMap param) {
        QueryWrapper<Explorer> wrapper = new QueryWrapper<>();
        return explorerMapper.selectList(wrapper);
    }

    /**
     * 通过编号查询
     *
     * @param primaryKey
     * @return
     */
    @Override
    @SneakyThrows
    public Explorer getById(String primaryKey) {
        Explorer explorer = explorerMapper.selectById(primaryKey);
        if (Objects.isNull(explorer)) {
            throw new BusinessException("编号不正确");
        }
        return explorer;
    }

    /**
     * 新增信息
     * @param explorer explorer
     */
    @Override
    @SneakyThrows
    public void saveOne(Explorer explorer) {
        LocalBeanUtil.setLoginUserInfo(explorer, UserThreadLocal.getUser().getUserName(), UserThreadLocal.getUser().getUnitId());
        int count = explorerMapper.insert(explorer);
        if (count != 1){
            throw new BusinessException("保存失败");
        }
    }

    /**
     * 更新信息
     * @param explorer explorer
     */
    @Override
    @SneakyThrows
    public void update(Explorer explorer) {
        LocalBeanUtil.updateLoginUserInfo(explorer, UserThreadLocal.getUser().getUserName());
        int count = explorerMapper.updateById(explorer);
        if (count != 1){
            throw new BusinessException("更新失败");
        }
    }

    /**
     * 删除信息
     * @param primaryKey id
     */
    @Override
    @SneakyThrows
    public void delete(String primaryKey) {
        Explorer explorer = explorerMapper.selectById(primaryKey);
        if (Objects.isNull(explorer)) {
            throw new BusinessException("未通过编号找到");
        }
        int count = explorerMapper.deleteById(primaryKey);
        if (count != 1){
            throw new BusinessException("删除失败");
        }
    }
}
